import { defineStore } from 'pinia'
import { getMenu } from '@/api/menu'
import type { menuResponse } from '@/api/menu/types'
import type { MenuState } from './types/types.ts'
import { SET_MENU, GET_MENU } from '@/utils/menu'

const useMenuStore = defineStore('menu', {
  state: (): MenuState => {
    return {
      menuList: GET_MENU(),
    }
  },
  actions: {
    async getMenus() {
      const res: menuResponse = await getMenu({
        userId: localStorage.getItem('userId') || '',
      })
      if (res.code === 200) {
        console.log(res)
        // 存储token
        const data = res.data.menuList
        SET_MENU(data)
        // 存储用户信息
        return 'OK'
      } else {
        return Promise.reject(new Error(res.msg))
      }
    },
  },
})

export default useMenuStore
